<HTML><HEAD> <!-- --------- Recursion --------- --> <SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers /* THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com Copyright (c)1998 by Charles River Media. All Rights Reserved. This applet can only be re-used or modifed by license holders of the JavaScript Cookbook CD-ROM. Credit must be given in the source code and this copyright notice must be maintained. If you do not hold a license to the JavaScript Cookbook, you may NOT duplicate or modify this code for your own use. Use at your own risk. No warranty is given or implied of the suitability of this applet for any specific application. Neither Erica Sadun nor Charles River Media will be held responsible for any unwanted effects due to the use of this applet or any derivative. */ // Fibonacci Recursion function xfib(n) { if (n < 0) // Fib(negative #'s) is not defined { return 0 } // stop recursion if n is 0 or 1. These are well defined // otherwise, solve for n-1 and n-2 and add the results if ((n == 0)||(n == 1)) n = 1 else n = xfib(n-1) + xfib(n-2) return n } // input validation function fib(aform) { var n = aform.number.value if (n > 16) alert("Please use a smaller number." + " The Fibonacci sequence is very memory intensive!") else { aform.number.value="fib("+n+") is "+xfib(n) aform.number.select() } } <!-- done hiding --></SCRIPT></HEAD> <BODY bgcolor="ffffff" onLoad="document.forms[0].number.focus();document.forms[0].number.select();return true"> <FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50 ALIGN = CENTER>Fibonacci</H1></FONT> <BLOCKQUOTE><FONT COLOR="770000"> JavaScript supports recursive function calls--that is, functions which call themselves to solve increasingly simpler problems. This example shows how to use recursion to create Fibonacci numbers. The higher the number, the longer it takes to compute--Fibonacci numbers are very memory intensive, especially using this recursive algorithm. Be patient with numbers greater than ten. </FONT> <FONT SIZE=4><BLOCKQUOTE> <FORM NAME="FibForm" onSubmit="fib(forms[0]);return false"> <INPUT TYPE="text" SIZE="15" NAME="number"> <INPUT TYPE="button" VALUE="FIB IT" onClick="fib(this.form);this.form.number.select();return true"> </FORM> </BLOCKQUOTE></FONT></BLOCKQUOTE> <FONT COLOR="007777"><H2>Discussion</H2></FONT> <FONT SIZE=4> The Fibonacci (<i>fih-boh-nah-chee</i>) sequence gives rise to many cheers including this favorite: <pre><null > One! One! Two! Three! Five! Eight!<br><null > Who do we appreciate?<br><null > Fibonacci! Fibonacci!<br><null > Yaaaaaay! Fibonacci!<br><null ></pre> The Fibonacci sequence starts off with "one" and "one", respectively the zeroth and first numbers of the sequence. Each successive number proceeds by adding the two previous numbers. The second Fibonacci number is two. The third is three. The fourth is five. The fifth is eight. The sixth is thirteen, and so on. </FONT></FONT> <FONT COLOR="770000"><PRE> // Fibonacci Recursion function xfib(n) { if (n < 0) // Fib(negative #'s) is not defined { return 0 } // stop recursion if n is 0 or 1. These are well defined // otherwise, solve for n-1 and n-2 and add the results if ((n == 0)||(n == 1)) n = 1 else n = xfib(n-1) + xfib(n-2) return n } </PRE></FONT> </FONT></BLOCKQUOTE> <h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5> </BODY> </HTML>